package org.chen.ctrip.controller;

import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.chen.common.ctrip.entity.CustomDayContent;
import org.chen.common.result.ResultVO;
import org.chen.ctrip.service.CustomDayContentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/custom-content")
@Tag(name = "不重要的接口", description = "不重要的接口")
public class CustomDayContentController {

    @Autowired
    private CustomDayContentService customDayContentService;

    @GetMapping("/platforms")
    public ResultVO<Map<String, List<CustomDayContent>>> getAllPlatformContents() {
        try {
            List<CustomDayContent> allContents = customDayContentService.list();
            Map<String, List<CustomDayContent>> platformMap = allContents.stream()
                    .collect(Collectors.groupingBy(CustomDayContent::getPlatform));
            
            return ResultVO.success(platformMap);
        } catch (Exception e) {
            log.error("获取平台内容失败", e);
            return ResultVO.fail("获取平台内容失败: " + e.getMessage());
        }
    }

    @GetMapping("/platform/{platform}")
    public ResultVO<List<CustomDayContent>> getPlatformContents(@PathVariable String platform) {
        try {
            List<CustomDayContent> contents = customDayContentService.getContentByPlatform(platform);
            return ResultVO.success(contents);
        } catch (Exception e) {
            log.error("获取平台{}内容失败", platform, e);
            return ResultVO.fail("获取平台内容失败: " + e.getMessage());
        }
    }

    @PostMapping
    public ResultVO<CustomDayContent> saveOrUpdateContent(@RequestBody CustomDayContent content) {
        try {
            CustomDayContent savedContent = customDayContentService.saveOrUpdateContent(content);
            return ResultVO.success(savedContent);
        } catch (Exception e) {
            log.error("保存自定义内容失败", e);
            return ResultVO.fail("保存失败: " + e.getMessage());
        }
    }

    @GetMapping("/{id}")
    public ResultVO<CustomDayContent> getContent(@PathVariable String id) {
        try {
            CustomDayContent content = customDayContentService.getById(id);
            if (content == null) {
                return ResultVO.fail("内容不存在");
            }
            return ResultVO.success(content);
        } catch (Exception e) {
            log.error("获取内容失败", e);
            return ResultVO.fail("获取内容失败: " + e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public ResultVO deleteContent(@PathVariable String id) {
        try {
            customDayContentService.removeById(id);
            return ResultVO.success("操作成功");
        } catch (Exception e) {
            log.error("删除内容失败", e);
            return ResultVO.fail("删除失败: " + e.getMessage());
        }
    }

    @GetMapping("/matching")
    public ResultVO<CustomDayContent> getMatchingContent(
            @RequestParam String platform,
            @RequestParam String dayType,
            @RequestParam(required = false) String departureCity,
            @RequestParam(required = false) String destinationCity,
            @RequestParam(required = false) Integer durationDays) {
        try {
            CustomDayContent content = customDayContentService.getMatchingContent(
                    platform, dayType, departureCity, destinationCity);
            return ResultVO.success(content);
        } catch (Exception e) {
            log.error("获取匹配内容失败", e);
            return ResultVO.fail("获取匹配内容失败: " + e.getMessage());
        }
    }

    @PostMapping("/batch")
    public ResultVO<List<CustomDayContent>> batchSaveContents(@RequestBody List<CustomDayContent> contents) {
        try {
            List<CustomDayContent> savedContents = contents.stream()
                    .map(customDayContentService::saveOrUpdateContent)
                    .collect(Collectors.toList());
            return ResultVO.success(savedContents);
        } catch (Exception e) {
            log.error("批量保存内容失败", e);
            return ResultVO.fail("批量保存失败: " + e.getMessage());
        }
    }

    @PostMapping("/{id}/toggle")
    public ResultVO<CustomDayContent> toggleContent(@PathVariable String id) {
        try {
            CustomDayContent content = customDayContentService.getById(id);
            if (content == null) {
                return ResultVO.fail("内容不存在");
            }
            
            content.setEnabled(!content.getEnabled());
            CustomDayContent savedContent = customDayContentService.saveOrUpdateContent(content);
            return ResultVO.success(savedContent);
        } catch (Exception e) {
            log.error("切换内容状态失败", e);
            return ResultVO.fail("操作失败: " + e.getMessage());
        }
    }
}